<%--
  Created by IntelliJ IDEA.
  User: 彭辉明
  Date: 2024/5/23
  Time: 10:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<style>
    body {
        font-family: Arial, sans-serif;
        background-color: #f0f0f0;
        margin: 0;
        padding: 0;
    }

    .form-container {
        width: 300px;
        margin: 50px auto;
        padding: 20px;
        background-color: white;
        border-radius: 5px;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }

    h1 {
        text-align: center;
        margin-bottom: 20px;
    }

    input[type="text"],
    textarea {
        display: block;
        width: 100%;
        padding: 10px;
        margin-bottom: 15px;
        border: 1px solid #ddd;
        border-radius: 3px;
        box-sizing: border-box;
    }

    textarea {
        height: 100px;
        resize: vertical;
    }

    input[type="button"] {
        width: 100%;
        padding: 10px;
        background-color: #4CAF50;
        color: white;
        border: none;
        cursor: pointer;
        border-radius: 3px;
    }

    input[type="button"]:hover {
        background-color: #45a049;
    }

    .error-message {
        color: red;
        margin-top: 5px;
        display: none;
    }
</style>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<div class="form-container">
    <h1>新增详细信息</h1>
    <form id="branchForm">
        <label for="name">网点名称：</label>
        <input type="text" id="name" required>
        <div class="error-message" id="nameError">请输入网点名称</div>

        <label for="license">营业执照编号：</label>
        <input type="text" id="license" required>
        <div class="error-message" id="licenseError">请输入营业执照编号</div>

        <label for="cityArea">所在地区：</label>
        <input type="text" id="cityArea" required>
        <div class="error-message" id="cityAreaError">请选择所在地区</div>

        <label for="address">网点地址：</label>
        <input type="text" id="address" required>
        <div class="error-message" id="addressError">请输入网点地址</div>

        <label for="telephone">联系电话：</label>
        <input type="text" id="telephone" required>
        <div class="error-message" id="telephoneError">请输入联系电话</div>

        <label for="introduction">网点简介：</label>
        <textarea id="introduction" required></textarea>
        <div class="error-message" id="introductionError">请输入网点简介</div>

        <input type="button" value="提交" id="addBranches">
    </form>
</div>
</body>
</html>
<script>
    $('#addBranches').on('click',function () {
        let name = $('#name').val().trim();
        let license = $('#license').val().trim();
        let cityArea = $('#cityArea').val().trim();
        let address = $('#address').val().trim();
        let telephone = $('#telephone').val().trim();
        let introduction = $('#introduction').val().trim(); // 修改为.val()以正确获取textarea的值

        // 非空验证
        if (!name || !license || !cityArea || !address || !telephone || !introduction) {
            alert('所有字段均为必填项，请确保没有遗漏！');
            return; // 如果有空字段，则阻止表单提交
        }
        $.ajax({
            url:'/branches/branches.action',
            type:'post',
            dataType: 'json',
            data:{
                opr:'selectByLicense',
                license:license
            },
            success:function (data){
                if(data!=null){
                    alert('该营业执照编号已存在')
                }else {
                    $.ajax({
                        url:'/branches/branches.action',
                        type:'post',
                        data:{
                            opr:'add',
                            name:name,
                            license:license,
                            cityArea:cityArea,
                            address:address,
                            telephone:telephone,
                            introduction:introduction
                        },
                        success:function (data) {
                            if(data==1){
                                alert('新增成功')
                                window.location.href = 'Branches.jsp'
                            }else {
                                alert('新增失败')
                            }
                        }
                    })
                }
            },
            error:function (){
            }
        })
    })
</script>
